/**
 * Created by zheng on 2018/7/2.
 */
var deleteindex = null
//当前保单所分配的状态ID
var state = 1

$(function () {
    //var binbin = 0;
    $('#dg').datagrid({
        url: '/insurancebill/queryOdd?state=' + state,
        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
         //单选
         singleSelect: true,
        //select: true,
        columns: [[
            {field: 'x', checkbox: 'true'},
            {field: 'oddNumber', title: '保单单号', width: 100},
            {
                field: 'applicant', title: '投保人', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }

            },
            {field: 'vdate', title: '申请日期', width: 100},
            {field: 'limitYear', title: '保险时长', width: 100},
            // {field: 'inputUser.name', title: '业务员', width: 100},
            {
                field: 'inputUser', title: '业务员', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.username : "";
                }

            },
            {field: 'totalPrice', title: '保险总金额', width: 100}
        ]],
        //新增
        border: false,
        cls: "table table-very table-basic"
    })


    //准备输入的对话弹框
    $('#dd').dialog({
        title: 'My Dialog',
        width: 700,
        height: 700,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb"
    })
    //订单详细
    $('#billDetail').dialog({
        title: 'My Dialog',
        width: 700,
        height: 700,
        closed: true,
        modal: true
    })
    //<!-- 准备投保人详情-->
    $('#applicantItem').dialog({
        title: 'My Dialog',
        width: 400,
        height: 500,
        closed: true,
        // cache: false,
        modal: true
    })

    //将工具栏所有的函数集合在一个对象中
    var cmdObj;
    cmdObj = {
        //刷新emp页面
        reload: function () {
            $('#dg').datagrid("load")
        },
        //添加
        add: function () {
            //清除以前的数据
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "员工添加")
        },
        //改变emp状态
        changState: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $.messager.confirm('确认', '您确认想要提交数据吗？', function (r) {
                if (r) {
                    $.get("/insurancebill/changState", {id: row.id, state: 3}, function (data) {
                        if (data.succeed) {
                            $.messager.alert("温馨提示", "提审成功");
                            $('#dg').datagrid("reload")
                        } else {
                            $.messager.alert("温馨提示", "提审失败");
                        }
                    })
                }
            });
        },
        //删除产品
        removeodditem: function () {
            if (deleteindex == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            } else {
                $('#showproductInfo_datagrid').datagrid('deleteRow', deleteindex)

            }
        },
        edit: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $('#ff').form("clear");
            $('#dd').dialog("open")
            $('#dd').dialog("setTitle", "查看保单信息")
            //清除以前的数据

            //总价显示
            var totalPrice = row.totalPrice != null ? row.totalPrice : 0
            $("#showtotalAmount").text(totalPrice);
            //保险年限
            //binbin = row.limitYear;
            $("#showlimitYear_input").textbox('setValue', row.limitYear);


            //回显
            if (row.car) {
                $("#carId").val(row.car.id);
            }
            $('#ff').form('load', row.car);
            //单号处理回显
            $("#billId").val(row.id);


            //暂存表中的dategrid
            $('#showproductInfo_datagrid').datagrid({
                fitColumns: true,
                height: 200,
                //分页条
                pagination: true,
                // fit:true,
                //工具栏
                toolbar: "#odditem",
                columns: [[
                    {field: 'x', checkbox: true},
                    {field: 'id', title: '编号', hidden: true},
                    {field: 'sn', title: '保险编号', width: 100},
                    {field: 'name', title: '保险产品', width: 100},
                    {field: 'annualfee', title: '保险年费', width: 100}
                ]],
                onSelect: function (index, row) {
                    //双击删除一个
                    deleteindex = index;
                    console.log(1111)
                    console.log(deleteindex)
                    //$('#allPermissions').datagrid('deleteRow', index);

                },
            })

            //产品机构的的回显
            $.get("/product/queryProductListByBillId?billId=" + row.id,
                function (data) {
                    console.log(data);
                    if (data) {
                        $("#showproductInfo_datagrid").datagrid('loadData', data);
                        $("#agency").combobox('setValue', data[0].orgName);
                    }
                })


        },
        queryOddItem: function () {

            var row = $('#dg').datagrid("getSelected");
            console.log(row)
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $('#billDetailForm').form("clear");
            $('#billDetail').dialog("open")
            $('#billDetail').dialog("setTitle", "查看保单信息")
            console.log("row");
            console.log(row);
            //清除以前的数据

            //总价显示
            var totalPrice = row.totalPrice != null ? row.totalPrice : 0
            $("#showtotalAmount_r").text(totalPrice);
            //保险年限
            console.log("保险年限");
            console.log(row.limitYear);
            //binbin = row.limitYear;
            $("#showlimitYear_input_r").textbox('setValue', row.limitYear);


            //回显
            if (row.car) {
                $("#carId").val(row.car.id);
            }
            $('#billDetailForm').form('load', row.car);
            //单号处理回显
            $("#billId").val(row.id);

            //产品机构的的回显
            $.get("/product/queryProductListByBillId?billId=" + row.id,
                function (data) {
                    console.log("data");
                    console.log(data);
                    if (data.length > 0) {
                        $("#billDetail_datagrid").datagrid('loadData', data);
                        if (data[0] != null) {
                            $("#agency_r").combobox('setValue', data[0].orgName);

                        }
                        console.log(data[1]);
                        console.log(data[0].orgName);
                        console.log(data[0].organizationId);
                    }
                })
            //暂存表中的dategrid
            $('#billDetail_datagrid').datagrid({
                fitColumns: true,
                height: 200,
                //分页条
                pagination: true,
                // fit:true,
                columns: [[
                    {field: 'id', title: '编号', hidden: true},
                    {field: 'sn', title: '保险编号', width: 100},
                    {field: 'name', title: '保险产品', width: 100},
                    {field: 'annualfee', title: '保险年费', width: 100}
                ]]
            })

        },
        queryApplicantItem: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $('#applicantItemForm').form("clear");
            $('#applicantItem').dialog("open")
            $('#applicantItem').dialog("setTitle", "查看投保单明细")
            //处理部门回显
            /*    if (row.dept) {
             row["dept.id"] = row.dept.id;

             }*/

            //客户数据字典编辑回显

            $.get("/client/get?id=" + row.applicant.id,
                function (data) {
                    if (data.clientSource != null) {
                        $("#clientSource").combobox("setValue", data.clientSource.name);

                    }
                    if (data.salaryLevel != null) {
                        $("#salary").combobox("setValue", data.salaryLevel.name);

                    }
                    if (data.job != null) {
                        $("#job").combobox("setValue", data.job.name);

                    }
                    //$("#clientSource").combobox("setValue", "kkkkk");

                    console.log("字典");
                    console.log(data);
                    console.log(data.clientSource);
                    console.log(data.salaryLevel);
                    console.log(data.job);
                });
            //字典end

            //回显
            $('#applicantItemForm').form('load', row.applicant);
            console.log(row);

            /*      //客户来源数据的回显
             $.get("/applicant/queryRoleIdsByEmpId?empId=" + row.id,
             function (data) {
             $("#clientSource").combobox("setValues", data);
             })
             //职业数据的回显
             $.get("" + row.id,
             function (data) {
             $("#job").combobox("setValues", data);
             })
             //薪资水平数据的回显
             $.get("" + row.id,
             function (data) {
             $("#salary").combobox("setValues", data);
             })*/

        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue")
            var endDate = $("#endDate").textbox("getValue");
            /*          var state = Java.type("cn.wolfcode.crm.util.CRMConstant.Insurancebill.STATE_WAIT_ADUIT");
             console.log("state");
             console.log(state);*/
            $('#dg').datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
                state: state
            });
        },
        //保存数据[按钮]
        save: function () {
            //调用算钱方法,核实价格
            account();
            // $("#showproductInfo_datagrid").datagrid("clear");
            var url = "/insurancebill/update";
            //提交表单
            $('#ff').form('submit', {
                url: url,
                onSubmit: function (param) {
                    //提交非同名的数据 role[0].id
                    var ids = $("#showproductInfo_datagrid").datagrid('getSelections');
                    if (ids) {
                        for (var i = 0; i < ids.length; i++) {
                            console.log(ids[i].id);
                            param["productIds[" + i + "]"] = ids[i].id;
                        }
                    }
                    console.log("ids");
                    console.log(ids);
                    /*  param["applicant.id"] = $("#clientDg").datagrid("getSelected").id;
                     console.log($("#clientDg").datagrid("getSelected").id);
                     param["applicant.name"] = $("#name").textbox("getValue");
                     param["applicant.gender"] = $("#gender").textbox("getValue");
                     param["applicant.identityCard"] = $("#identityCard").textbox("getValue");
                     param["applicant.phone"] = $("#phone").textbox("getValue");
                     param["applicant.address"] = $("#address").textbox("getValue");
                     console.log($("#address").textbox("getValue"));*/
                    param["car.brand"] = $("#brand").textbox("getValue");
                    param["car.id"] = $("#carId").val();
                    console.log("carID");
                    param["car.number"] = $("#number").textbox("getValue");
                    param["car.displacement"] = $("#displacement").textbox("getValue");
                    param["car.classify"] = $("#classify").combobox("getValue");
                    param["car.buydate"] = $("#buyDate").datebox("getValue");
                    param["car.evaluate"] = $("#evaluate").textbox("getValue");
                    param["car.carOwner"] = $("#carname").textbox("getValue");
                    param["car.model"] = $("#model").textbox("getValue");
                    param["car.carsize"] = $("#carsize").textbox("getValue");
                    // param["car.id"] = $("#carDg").datagrid("getSelected").id;
                    console.log($("#showtotalAmount").text());
                    param["totalPrice"] = $("#showtotalAmount").text();
                    param["limitYear"] = $("#showlimitYear_input").val();
                },
                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "更新所编辑的内容成功");
                        //关闭对话框
                        $('#dd').dialog("close");
                        //刷新datagrid
                        $('#dg').datagrid("reload")
                    }
                }
            });
        },
        addProduct: function () {
            var id = $("#agency").combobox("getValue")
            console.log(id);
            if (id) {
                $("#showproductInfo_datagrid").datagrid('options').url = '/product/queryByOrganizationId?id=' + id;
                // $('#showproductInfo_datagrid').datagrid('selectAll');
                $("#showproductInfo_datagrid").datagrid('reload');
                $('#showproductInfo_datagrid').datagrid('selectAll');

            } else {
                $.messager.alert("温馨提示", "请选择一个机构")
            }

        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        }


    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

    //算钱的
    /*   $("#showlimitYear_input").textbox({
     value:binbin,
     onChange: function () {
     var totalAmount = 0;
     $('#showproductInfo_datagrid').datagrid('selectAll');
     var rows = $("#showproductInfo_datagrid").datagrid('getSelections');
     console.log(rows);
     if (rows) {
     for (var i = 0; i < rows.length; i++) {
     totalAmount = rows[i].annualfee * $("#showlimitYear_input").val() + totalAmount;
     }
     }
     $("#showtotalAmount").text(totalAmount);
     console.log(totalAmount);
     }
     })*/

    //调用算钱
    $("#showlimitYear_input").textbox('textbox').bind('blur', function () {
        account();
    });
    //算钱的
    function account() {

        //alert($("#showlimitYear_input").val())
        var totalAmount = 0;
        //也可默认全选
        // $('#showproductInfo_datagrid').datagrid('selectAll');
        var rows = $("#showproductInfo_datagrid").datagrid('getSelections');
        //console.log(rows);
        if (rows) {
            for (var i = 0; i < rows.length; i++) {
                totalAmount = rows[i].annualfee * $("#showlimitYear_input").val() + totalAmount;
            }
        }
        if (totalAmount != $("#showtotalAmount").text()) {

            $("#showtotalAmount").text(totalAmount);
        }
        //console.log(totalAmount);
    }

})







